//file: app.js
var ex = {
	Exception: function(message) {this.message = message;}
	,
	NotImplemented: function() {}
}
ex.NotImplemented.prototype = new ex.Exception('Not implemented');

// usage
// var el = dom.el('idName');
// var el = dom(iframes['test'].document).el('idName');
// var el = parent.dom.el('idName') will be equal parent.dom(document).el('idName');
var dom = (function Dom(doc)
{
	Dom.el = function(elName)
	{
		doc = doc || document;
		return doc.getElementById(elName);
	}
	
	Dom.create = function(def, html)
	{
		doc = doc || document;
		var regName = /^(\w+)./i;
		var regClass = /\.([^#]+)/i;
		var regId = /#(\w+)/i;
		var name = def.match(regName);
		
		if ( name[1] )
		{
			var el = doc.createElement(name[1]);
			var clsName = def.match(regClass);
			var idName = def.match(regId);
			
			clsName && clsName[1] && (el.className = clsName[1]);
			idName && idName[1] && (el.id = idName[1]);
		
			html && (el.innerHTML = html);
		}
		else throw 'Invalid parameter passed!!!';
		
		return el;
	}
	
	Dom.set = function (el, name, value)
	{
		for ( i = 1; i < arguments.length; i += 2 )
		{
			var name = arguments[i];
			var value = arguments[i + 1];
			
			var style = el.style;
			
			name in style && (style[name] = value);
			name in el && (el[name] = value);
		}
		
		return el;
	}
	
	return Dom;
})();

function printc(str, cont)
{
	var regexp = /{([\w\d]+)}/ig;
	
	return str.replace(regexp, function (a,b) { return printc.repl(a, b, cont); });
}
printc.repl = function(source, data, cont)
{
	return cont[data];
}

var async = {
	onSuccess: function (data, textStatus, jqXHR)
	{
		var result = $(data).find('result');
		
		menu.closeDialog('insertWord');
		dialog.alert.show(result.text());
	}
	,
	invoke: function (funcName, data, onSuccess)
	{
		var onSuccess = onSuccess || async.onSuccess;
		jQuery.post(funcName, data, onSuccess, 'xml');
	}
}

function Command(name)
{
	this.name = name;
}
Command.prototype.id = '';
Command.prototype.exec = function() { throw new ex.NotImplemented(); };
Command.items = [];
Command.get = function(name)
{
	for ( var i = 0; i < this.items.length; i++ )
	{
		var item = this.items[i];
		if ( item.name == name )
		{
			return item;
		}
	}
		
	throw printc('Command: \'{0}\' not found!!!', [name]);
}

function EditCommand()
{
}
EditCommand.prototype = new Command('edit');
EditCommand.prototype.exec = function(id)
{
	asyncEditBegin(id);
}
Command.items.push(new EditCommand());

function asyncSubmit(form)
{
	var data = $(form).serialize();
	
	async.invoke(form.action + "?async=insertWord", data);
	
	return false;
}

function asyncDelete(form)
{
	var lang = form.lang.value;
	var id = form.id.value;
	
	async.invoke(form.action + "?async=deleteWord", {
		language: lang,
		id: id
		});
		
	return false;
}

function asyncUpdate(form)
{
	var lang = form.language.value;
	var id = form.id.value;
	var word = form.word.value;
	var description = form.description.value;
	
	async.invoke(form.action + "?async=saveWord", {
		language: lang,
		id: id,
		word: word,
		description: description
		}
	);
	
	menu.closeDialog('editWord');
		
	return false;
}

function asyncEditBegin(id)
{
	async.invoke('?async=getWord', {
		language: 'en',
		id: id
		}
		,
		asyncEditEnd);
		
	return false;
}

function asyncEditEnd(data, textStatus, jqXHR)
{
		var result = $(data).find('result');
		var type = result.find('type');
		var details = result.find('details');
		
		if ( 'OK' == type.text() )
		{
			var data = {
				id: result.find('id').text(),
				word: result.find('word').text(),
				description: result.find('description').text()
			};
			
			menu.showDialog('editWord', data);
		}
		else
		{
			dialog.alert.show(result.text());
		}
}

function WebTable(id)
{
	this.id = id;
}

function delegate_onClick(object, func, args)
{
	return function () { return func.apply(this, args); };
}

WebTable.prototype.init = function ()
{
	var self = this;
	this.rowMenu = document.getElementById('rowMenu');
	this.domTable = document.getElementById(this.id);
	
	var rows = this.domTable.rows;
	for ( var i = 0; i < rows.length; i++ )
	{
		var row = rows[i];
		$(row).bind('click', delegate_onClick(self, function (tr)
		{
			var offset = $(tr).offset();
			self.rowMenu.style.display = 'block';
			self.rowMenu.style.top = offset.top - 1;
			self.rowMenu.style.left = offset.left - 1;
			self.rowMenu.style.height = 3;
			self.rowMenu.style.width = $(tr).innerWidth() - 1;
			$(self.rowMenu).find('div').unbind();
			$(self.rowMenu).find('div').bind('click', delegate_onClick(self, function(tr)
			{
				var editCmd = Command.get('edit');
				var id = tr.getAttribute('itemid');
				editCmd.exec(id);
			},[tr]));
		}, [row]));
	}
}

var dialog =
{
	alert: {
		show: function ()
		{
			switch( arguments.length )
			{
				case 2:
					$('#alertTitle').text(arguments[0]);
					$('#alertMessage').text(arguments[1]);
				break;
				default:
					$('#alertMessage').text(arguments[0]);
				break;
			}
			$('#alert').appendTo('body');
			$('#alert').fadeIn({
				effect: "fade",
				options: {},
				speed: 500,
				complete: function() {
					if ( $.browser.msie )
						this.style.removeAttribute('filter');
				}
			});
		},
		hide: function ()
		{
			$('#alert').fadeOut(200);
		}
	}
}

var menu = 
{
	showDialog: function(id, data)
	{
		if (id == 'editWord')
		{
			var form = dom.el('enUpdate');
			form.word.value = data.word;
			form.id.value = data.id;
			form.description.value = data.description;
		}
		$('#'+id).appendTo('body');
		$('#'+id).fadeIn({
				effect: "fade",
				option: {},
				speed: 500,
				complete: function() {
					if ( $.browser.msie )
						this.style.removeAttribute('filter');
				}
			});
	}
	,
	closeDialog: function(id)
	{
		$('#'+id).fadeOut(500);
	}
	,
	edit: function(id)
	{
		var editCmd = new EditCommand(id);
		editCmd.exec();
	}
};